package spring.core.examples.xml;

import java.io.IOException;
import java.io.InputStream;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.ResourceLoader;

/**
 * This class demonstrates how to use the Spring resources.
 */
public class Resources implements ResourceLoaderAware {

	/** The resource loader. */
	ResourceLoader resourceLoader;

	/**
	 * Instantiates a new resources.
	 */
	public Resources() {
		super();
	}

	/**
	 * Returns properties contained in a specific resource.
	 * 
	 * @return the my properties
	 */
	public Properties getMyProperties() {
		ClassPathResource myResource = (ClassPathResource) this.resourceLoader
				.getResource("classpath:my.properties");
		Properties myResult = new Properties();
		try {
			InputStream myInputStream = myResource.getInputStream();
			myResult.load(myInputStream);
			myInputStream.close();
		} catch (InvalidPropertiesFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return myResult;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.context.ResourceLoaderAware#setResourceLoader(org
	 * .springframework.core.io.ResourceLoader)
	 */
	public void setResourceLoader(ResourceLoader resourceLoader) {
		this.resourceLoader = resourceLoader;
	}

}
